package com.example.mynews;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;

/**
 * Created by 王聪 on 2018/5/21.
 */

class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
    private Context context;
    private NewsData newsData;

    public MyAdapter(Context context,NewsData newsData){
        this.context=context;
        this.newsData=newsData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.news_list_item,parent,false);
        MyViewHolder myViewHolder=new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final NewsData.ResultBean.DataBean dataBean=newsData.getResult().getData().get(position);
        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent();
                intent.setClass(context,ShowNewsActivity.class);
                intent.putExtra("NEWS_URL",dataBean.getUrl());
                intent.putExtra("NEWS_ID", dataBean.getUniquekey());
                context.startActivity(intent);
            }
        });
        holder.getTitle().setText(dataBean.getTitle());
        holder.getTitle().setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        holder.getAuthor().setText(dataBean.getAuthor_name());
        holder.getDate().setText(dataBean.getDate());
        Glide.with(context).load(Uri.parse(dataBean.getThumbnail_pic_s())).into(holder.getHead());
    }

    @Override
    public int getItemCount() {
        return newsData.getResult().getData().size();
    }

    public void dataChanged(NewsData newsData) {
        this.newsData=newsData;
        notifyDataSetChanged();
    }
}
